module IPFS.Client(
  Client(..),
  fromMultiaddr
) where
import Prelude

import Data.Default (class Default, def)
import Data.Either (Either)
import Data.Integral (fromIntegral)
import Data.Tuple.Nested ((/\))
import IPFS.Multiaddr as M
import IPFS.Multiaddr.IP (Port)

newtype Client = Client {
  host::String,
  port::Port,
  apiPath::String,
  protocol::String
}

instance showClient :: Show Client where
  show (Client c) = "{ host:" <> c.host <> ",port:" <> show c.port 
                       <> ",apiPath:" <> c.apiPath <> ",protocol:"<> c.protocol <>"}"


instance defaultClient :: Default Client where
  def = Client {
    host:"localhost",
    port:fromIntegral 5001,
    apiPath:"api/v0/",
    protocol:"http"
  }

fromMultiaddr::M.Multiaddr -> Either String Client
fromMultiaddr maddr = do
   (addr /\ p) <- M.nodeAddress maddr
   let (Client defClient) = def
   pure $ Client {host : addr,port : p,apiPath:defClient.apiPath,protocol:defClient.protocol}
 